package cas_spike_server;

import java.net.*;
import java.io.*;

public class SpikeServer {

	static ServerSocket server;
	protected final static int port = 32314;
	static Socket connection;

	static boolean first;
	static StringBuffer process;
	static String timeStamp;

	public static void main(String[] args) {
		try {
			server = new ServerSocket(port);
			System.out.println("SpikeServer initialized.");
			int character;
			while (true) {
				connection = server.accept();
				System.out.println(connection.getPort()); // Added by Adam

				BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
				InputStreamReader isr = new InputStreamReader(bis);
				StringBuffer stringBuffer = new StringBuffer();
				while ((character = isr.read()) != 13) {
					stringBuffer.append((char) character);
				}
				System.out.println(stringBuffer);

				timeStamp = new java.util.Date().toString();
				String returnCode = "SingleSocketServer repsonded at "
						+ timeStamp + (char) 13;
				BufferedOutputStream bos = new BufferedOutputStream(
						connection.getOutputStream());
				OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
				osw.write(returnCode);
				osw.flush();
			}
		} catch (IOException e) {
		}
		try {
			connection.close();
		} catch (IOException e) {
		}
	}
}